﻿// -----------------------------------------------------------------------
// <copyright file="SimpleJob.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Job
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;

    using Dhgms.TplHelper.Model.Info;
    using Dhgms.TplHelper.Model.Info.JobResult;
    using Dhgms.TplHelper.Model.TaskHelper;

    /// <summary>
    /// Simple Job is for a Job that runs a single task
    /// </summary>
    public abstract class SimpleJob<TJobResultClass, TJobSettings, TTaskClass, TTaskResultClass> : Base<TJobResultClass, TJobSettings>
        where TJobResultClass : SingleTask<TTaskResultClass>, new()
        where TJobSettings : ConfigurationSection
        where TTaskClass : BaseTaskHelper<TTaskResultClass>
        where TTaskResultClass : TaskResultBase, new()
    {
        /// <summary>Initializes a new instance of the <see cref="SimpleJob{TJobResultClass,TJobSettings,TTaskClass,TTaskResultClass}"/> class.</summary>
        /// <param name="guid">The unique id for the job.</param>
        /// <param name="friendlyName">The friendly name.</param>
        protected SimpleJob(Guid guid, string friendlyName)
            : base(guid, friendlyName)
        {
        }

        /// <summary>
        /// The on execute event, where the donkey work should be done
        /// </summary>
        /// <param name="applicationSettings">The application settings</param>
        /// <returns>Job Result Data</returns>
        protected override TJobResultClass OnExecute(TJobSettings applicationSettings)
        {
            var jobResult = new TJobResultClass();
            var task = this.GetTask();
            jobResult.TaskResult = Model.Helper.Task.Execute<TTaskClass, TTaskResultClass>(task);
            return jobResult;
        }

        /// <summary>
        /// Gets the task instance to be run
        /// </summary>
        /// <returns>The task</returns>
        protected abstract TTaskClass GetTask();
    }
}
